﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;
using System.Net.Mail;
using System.Configuration;

namespace outREACH
{
    public partial class MakingDonation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                Label lbl1 = (Label)Master.FindControl("Label1");
                lbl1.Text = "Welcome " + Session["Username"].ToString() + "!";
                Label lbl2 = (Label)Master.FindControl("Name");
                Users u = Users.GetName(Session["Username"].ToString());
                lbl2.Text = u.firstName + " " + u.lastName;
                Label lbl3 = (Label)Master.FindControl("Country");
                lbl3.Text = Session["country"].ToString();
                Button btn3 = (Button)Master.FindControl("Button4");
                if (User.IsInRole("admin"))
                {
                    btn3.Visible = true;
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        protected void Donate_Click(object sender, EventArgs e)
        {
            FinDonation fd = new FinDonation();
            fd.userName = Session["UserName"].ToString();
            fd.creditCardName = TextBox19.Text;
            fd.creditCardNo = TextBox4.Text;
            fd.CVV = Convert.ToInt32(TextBox20.Text);
            fd.paymentType = DropDownList4.SelectedValue.ToString();
            fd.country = DropDownList8.SelectedValue.ToString();
            fd.city = TextBox15.Text;
            fd.billingAdd = TextBox12.Text;
            fd.zipCode = TextBox13.Text;
            fd.email = TextBox10.Text;
            fd.hp = Convert.ToInt32(TextBox8.Text);
            fd.home = Convert.ToInt32(TextBox9.Text);
            fd.section = DropDownList5.SelectedValue;
            fd.countryToDonate = DropDownList6.SelectedValue;
            fd.NGOs = DropDownList9.SelectedValue.ToString();
            fd.amount = Convert.ToInt32(TextBox14.Text);
            DateTime myDateTime = DateTime.Now;
            DateTime myDateTime3 = DateTime.Today;
            fd.dateTime = myDateTime;
            fd.category = "Donation";
            FinDonation.newDonation(fd);

            FinDonationHistory.insertFinHistory(fd.userName, fd.dateTime, fd.section, fd.countryToDonate, fd.amount, fd.paymentType, myDateTime3);
            DateTime myDateTime2 = System.Data.SqlTypes.SqlDateTime.MinValue.Value;
            string name = fd.userName;
            Annoucement.InsertAnnoucement(fd.userName, "", "", fd.dateTime, fd.category, "", "", "", "", name, fd.amount, fd.countryToDonate, fd.category);
            FinancialAnnoucement.InsertFinAnnoucement(fd.userName, fd.amount, fd.countryToDonate, fd.section, fd.dateTime);

            //SEND EMAIL TO DONOR ONCE TRRANSACTION IS SUCCESSFUL

            MailMessage message = new MailMessage();
            message.From = new MailAddress("outreach360@gmail.com");
            message.To.Add(new MailAddress(TextBox10.Text));
            message.Subject = "Thank you for Donating!";
            message.Body = "Dear" + " " + TextBox19.Text + "," + "\n" + "\nThank You for making a donation through our website. We will let you know by email on how your contribution has made a difference!" + "\n" + "\nWith Regards," + "\nOutreach Team"; //(TextBox2.Text) ","


            // SMTP Configuration
            SmtpClient smtp = new SmtpClient();

            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("outreach360@gmail.com", "360outreach");
            // MAIL MESSAGE CONFIGURATION

            // SEND EMAIL
            smtp.Send(message);

            //Alert.Show("Thanks for your donation! Your donation would help many people in need! =)");
            //Response.Redirect("~/Donation/DonationMem.aspx");

            Response.Redirect("~/Donation_NonDCs/ResponseDon.aspx");
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void TextBox4_TextChanged(object sender, EventArgs e)
        {
        
            
        }

        protected void TextBox14_TextChanged(object sender, EventArgs e)
        {

        }
        

    }
}
